package com.example.demo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.bean.currency.Sucess;
import com.example.demo.bean.currency.UserSelect;
import com.example.demo.bean.login.LoginBean;
import com.example.demo.bean.login.LoginPamas;
import com.example.demo.bean.user.UserBean;
import com.example.demo.mapper.login.UserMapper;
import com.example.demo.utils.GetToken;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/api")
@RestController
@SpringBootApplication
public class UserList {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("userDel")
    public Sucess userDel(@RequestBody UserBean userBean){
        Sucess sucess = new Sucess();

        if (userBean == null || userBean.getId() == null) {
            sucess.setMessage("请传入有效的id");
            sucess.setCode(-100);
            return sucess;
        }

        Long id = userBean.getId();
        if(id == 1) {
            sucess.setMessage("不能删除管理员");
            sucess.setCode(-200);
            return sucess;
        }
        try {
        boolean is =    userMapper.deleteById(id)>0;
        if(is) {
            sucess.setCode(200);
            sucess.setMessage("用户删除成功");
        }else {
            sucess.setCode(100);
            sucess.setMessage("删除用户失败");
        }

        } catch (Exception e) {
            sucess.setCode(-200);
            sucess.setMessage("删除用户时发生错误：" + e.getMessage());
            // 记录日志
        }

        return sucess;
    }
    @PostMapping(value = "saveOrUpdateUser")
    public  Sucess saveOrUpdateUser(@RequestBody UserBean userBean) {
        Sucess sucess = new Sucess();
        // 判断是新增还是更新
        if (userBean.getId() == null || userBean.getId() <= 0) {
            // 新增操作
            int insertResult = userMapper.insert(userBean);
            if (insertResult > 0) {
                sucess.setCode(200);
                sucess.setMessage("更新成功");
                return sucess;
            } else {
                sucess.setCode(100);
                sucess.setMessage("更新失败");
                return sucess;
            }
        } else {
            // 更新操作
            QueryWrapper<UserBean> updateWrapper = new QueryWrapper<>();
            updateWrapper.eq("id", userBean.getId());
            System.out.print(userBean.getId());
            int updateResult = userMapper.update(userBean, updateWrapper);
            if (updateResult > 0) {
                sucess.setCode(200);
                sucess.setMessage("更新成功");
                return sucess;
            } else {
                sucess.setCode(100);
                sucess.setMessage("更新失败");
                return sucess;
            }
        }
    }
    @PostMapping("userList")
    public <T> T userList(@RequestBody UserSelect<UserBean> userBean, HttpServletRequest request) {
        boolean is = isTokenValid(request.getHeader("token"));
        if (!is) {
            return (T) "token错误";
        }
        QueryWrapper<UserBean> wrapper = new QueryWrapper<UserBean>();
        if (userBean.getQueryObj().getUsername() != null) {
            wrapper.like("username", userBean.getQueryObj().getUsername());
        }
        Page<UserBean> page = new Page<>(userBean.getPageNumber(), userBean.getPageSize()); // 分页参数，页码和每页数量
        Page<UserBean> result = userMapper.selectPage(page, wrapper);
        return (T) result;

    }

    private boolean isTokenValid(String token) {
        // 在这里实现 token 的验证逻辑，使用 userMapper 查询相关数据
        QueryWrapper<UserBean> wrapper = new QueryWrapper<>();
        wrapper.eq("token", token);
        List<UserBean> userList = userMapper.selectList(wrapper);
        return !userList.isEmpty();
    }
//    @PostMapping("addUser")
//    public Sucess addUser(@RequestBody UserBean user,HttpServletRequest request){}
}
